Ext.define('Seq.qunee.DataFlow', {
    extend: 'Seq.qunee.Container',
    xtype: 'quneedataflow',

    initGraph(view) {
        this.initAlarm();
        return new Q.Graph(view.getId());
    },

    initAlarm() {
        if (Q.Element.prototype._initBalloon) {
            return;
        }

        Q.Element.prototype._alarmLevels = {
            fault: {color: Q.toColor(0xeeff0000), name: "故障"},
            alarm: {color: Q.toColor(0xeeffaa00), name: "警报"},
            warn: {color: Q.toColor(0xeeffff00), name: "警告"}
        };

        Q.Element.prototype._initBalloon = function () {
            var alarmUI = new Q.LabelUI();
            alarmUI.position = Q.Position.CENTER_TOP;
            alarmUI.alignPosition = Q.Position.LEFT_TOP;
            alarmUI.anchorPosition = Q.Position.LEFT_BOTTOM;
            alarmUI.border = 1;
            alarmUI.backgroundGradient = Q.Gradient.LINEAR_GRADIENT_VERTICAL;
            alarmUI.padding = new Q.Insets(2, 5);
            alarmUI.showPointer = true;
            alarmUI.offsetY = -10;
            alarmUI.offsetX = -10;
            alarmUI.fontSize = 5;
            alarmUI.rotatable = false;
            alarmUI.showOnTop = true;
            this._alarmBalloon = alarmUI;
        };

        Q.Element.prototype._checkAlarm = function () {
            var me = this;
            if (!me._alarmBalloon) {
                me._initBalloon();
            }
            if (me._alarm) {
                var level = me._alarmLevels[me._alarm.level];
                me._alarmBalloon.data = me._alarm.message;
                if(level) {
                    me._alarmBalloon.backgroundColor = level.color;
                    me.setStyle(Q.Styles.RENDER_COLOR, level.color);
                }
                if (me.addUI(me._alarmBalloon) === false) {
                    me.invalidate();
                }
            } else {
                me.removeUI(me._alarmBalloon);
            }
        };

        Object.defineProperties(Q.Element.prototype, {
            alarm: {
                get() {
                    return this._alarm;
                },
                set(value) {
                    this._alarm = value;
                    this._checkAlarm();
                }
            }
        });
    },

    setData(value) {
        let graph = this.graph;
        for (let alarm of value) {
            let e = graph.graphModel.getById(alarm.id);
            if (e) {
                e.alarm = alarm;
            }
        }
    },

    createNode(id, name, image) {
        let node = new Q.Node(name);
        node.id = id;
        node.image = image || Q.Graphs.server;
        this.graph.graphModel.add(node);
        return node;
    },

    createNodes(nodes) {
        for (let node of nodes) {
            let gNode = this.createNode(node.id, node.name, node.image);
            gNode.data = node;
        }
    },

    createEdge(id, name, from, to, edgeType) {
        let edge = new Q.Edge(from, to, name);
        edge.id = id;
        edge.setStyle(Q.Styles.LABEL_RADIUS, 0);
        edge.setStyle(Q.Styles.LABEL_ROTATABLE, false);
        edge.setStyle(Q.Styles.LABEL_ANCHOR_POSITION, Q.Position.CENTER_MIDDLE);
        edge.setStyle(Q.Styles.ARROW_TO, true);
        edge.edgeType = edgeType || Q.Consts.EDGE_TYPE_ORTHOGONAL_HORIZONTAL;
        this.graph.graphModel.add(edge);
        return edge;
    },

    createEdges(edges) {
        let graphModel = this.graph.graphModel;
        for (let edge of edges) {
            let from = graphModel.getById(edge.from), to = graphModel.getById(edge.to);
            from && to && this.createEdge(edge.id, edge.name, from, to, edge.edgeType);
        }
    },

    initNodes(data) {
        let me = this;
        me.graph.clear();
        me.createNodes(data.nodes);
        me.createEdges(data.edges);

        let layouter = new S.Layout.Hierarchy(me.graph);
        layouter.doLayout({
            byAnimate: true,
            layout: 'hbox',
            xGap: 300,
            yGap: 80,
            callback: function () {
                me.graph.zoomToOverview();
            }
        });
    },

    clear() {
        this.graph.clear();
    }

});